package com.zanfeng.signleplatform.dao.persistence.dao;

import com.zanfeng.signleplatform.api.result.modules.module.ModuleResult;
import com.zanfeng.signleplatform.base.BaseMapper;
import com.zanfeng.signleplatform.dao.persistence.entity.Module;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

public interface ModuleMapper extends BaseMapper<Module> {

    //  列表查询
    List<com.zanfeng.signleplatform.api.result.modules.module.ModuleResult> queryModuleList(Map paramMap);

    //  根据ID列表逻辑删除
    void deleteModuleListByIdList(List<Integer> idList);

    //  查询节点子元素数量
    int countChildrenModuleListByIdList(List<Integer> idList);

    //  列表查询
    @Select("select * from wt_module where `module_name` = #{moduleName} and `is_delete` = 0")
    Module findModuleByModuleName(@Param("moduleName") String moduleName);

    //  查询组织机构子节点
    @Select("select wm.*, ( select count(1) from wt_module where is_delete = 0 and module_parent_id = wm.id ) as childrenSize from wt_module as wm " +
            "   where wm.module_parent_id = #{parentId} and wm.is_delete = 0 " +
            "   order by wm.module_level , wm.order_num DESC")
    List<ModuleResult> queryModuleNodeList(String parentId);

    //  查询组织机构子节点
    @Select("SELECT wm.* FROM wt_account wa , wt_role_permission wrp , wt_permission wp , wt_module wm " +
            "WHERE wa.role_id = wrp.`role_id` AND wrp.`permission_id` = wp.`id` AND wp.`module_id` = wm.`id` " +
            "AND wa.`id` = #{accountId} GROUP BY wp.`module_id` ORDER BY wm.`module_level`, wm.`module_parent_id`, wm.`order_num` DESC")
    List<ModuleResult> queryModuleNodeListByAccountId(Integer accountId);

    //  查询全部组织机构子节点
    @Select("select wm.*, ( select count(1) from wt_module where is_delete = 0 and module_parent_id = wm.id ) as children_size from wt_module as wm " +
            "   where wm.is_delete = 0 " +
            "   order by wm.module_level , wm.order_num DESC")
    List<ModuleResult> queryAllModuleNodeList();

    //  查询全部激活状态的组织机构子节点
    @Select("select wm.*, ( select count(1) from wt_module where is_delete = 0 and module_parent_id = wm.id ) as children_size from wt_module as wm " +
            "   where wm.is_delete = 0 and wm.is_publish = 1 " +
            "   order by wm.module_level , wm.order_num DESC")
    List<ModuleResult> queryAllActiveModuleNodeList();
}